#include <boost/interprocess/ipc/message_queue.hpp>   
#include <iostream>   
#include <vector>   

#include "msg_type.h"

using namespace boost::interprocess;  


int main ()  
{ 
    struct timespec time1 = {0, 0}; 
    struct timespec time2 = {0, 0};

    float temp;
    MSG_DATA tmpmsg;

    struct  timeval   tv_begin,tv_end;


    try{  

        #if 1
      /*Open a message queue.   */
      message_queue mq  
         (open_only        //only create   
         ,"message_queue"  //name   
         );  
         #endif      
        #if 0
        /*Erase previous message queue   */
        message_queue::remove("message_queue");  
        /*Create a message_queue.  */
        message_queue mq  
            (create_only               //only create   
            ,"message_queue"           //name   
            ,def_TEST_CNT                       //max message number   
            //,sizeof(int)               //max message size   
            ,sizeof(MSG_DATA)               //max message size   
            
            );
            #endif  

        #if 0
        /*Send 100 numbers   */
        gettimeofday(&tv_begin,NULL);
        clock_gettime(CLOCK_REALTIME, &time1);
        for(int i = 0; i < def_TEST_CNT; ++i){  
            mq.send(&i, sizeof(i), 0);  
        }
        clock_gettime(CLOCK_REALTIME, &time2);
        gettimeofday(&tv_end,NULL);
        #endif

        #if 1
        /*Send 100 numbers   */
        tmpmsg.mtype=long(4);
        while(1)
        {
        gettimeofday(&tv_begin,NULL);
        clock_gettime(CLOCK_REALTIME, &time1);
        for(int i = 0; i < def_TEST_CNT; ++i){ 
            tmpmsg.mtype=i+3;
            mq.send(&tmpmsg, sizeof(tmpmsg), 0);  
        }
        clock_gettime(CLOCK_REALTIME, &time2);
        gettimeofday(&tv_end,NULL);
        #endif

        temp = (time2.tv_nsec - time1.tv_nsec) ;
        long seconds = time2.tv_sec - time1.tv_sec;
        long nanoseconds = time2.tv_nsec - time1.tv_nsec;
        printf("Elapsed time: %ld seconds, %ld nanoseconds\n", seconds, nanoseconds);

        printf("time = %f ns\n", temp);  
    
        printf("tv_begin.sec:%d\n",tv_begin.tv_sec);

        printf("tv_begin.usec:%d\n",tv_begin.tv_usec);

        printf("tv_end.sec:%d\n",tv_end.tv_sec);

        printf("tv_end.usec:%d\n",tv_end.tv_usec);    
        printf("tv_end.usec-tv_begin.tv_usec:%d\n",tv_end.tv_usec-tv_begin.tv_usec);    
        
        sleep(5);
        break;
    }

        
    } 
    catch(interprocess_exception &ex){  
        std::cout <<"exp: "<< ex.what() << std::endl;  
        return 1;  
    }
    
    


    return 0;  

}  


